import os
import logging
from pydantic_settings import BaseSettings
from dotenv import load_dotenv

# 配置日志
logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger(__name__)

# 加载.env文件
logger.debug("开始加载.env配置文件...")
load_dotenv()


class Settings(BaseSettings):
    # 飞书配置
    feishu_app_id: str
    feishu_app_secret: str
    amendment_sheet_url: str
    amendment_row: int
    amendment_col: int
    manuscript_sheet_url: str
    manuscript_row: int
    manuscript_col: int

    # 火山模型配置
    volcano_api_key: str
    volcano_model: str  # 从.env获取，不再设置默认值

    class Config:
        env_file = ".env"
        env_file_encoding = 'utf-8'  # 确保正确的编码读取


# 读取配置
try:
    settings = Settings()
    logger.debug("所有配置参数加载完成")
    # 验证关键配置是否存在
    required_settings = [
        'feishu_app_id', 'feishu_app_secret', 'amendment_sheet_url',
        'manuscript_sheet_url', 'volcano_api_key', 'volcano_model'
    ]
    for setting in required_settings:
        if not getattr(settings, setting, None):
            logger.warning(f"配置项 {setting} 可能未正确设置")
except Exception as e:
    logger.critical(f"配置加载失败：{str(e)}")
    raise
